// pages/search/search.js
var searchData = require('../../data/search.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchGoods: [],//搜索商品
    keyword:'', //关键字
    pageSize:6,//分页大小
    pageIndex:1,//当前页面
    loadFlag:false,//load标识
    noData:false//无数据标识
    
  },

  initSearch:function(){
    this.setData({
      searchGoods: [],
      keyword: '',
      pageSize: 6,
      pageIndex: 1,
      loadFlag: false,
      noData: false
    });
  },

  onConfirm: function (e) {
     this.initSearch();
     var keyword = e.detail.value;
     this.setData({keyword:keyword});
     this.getSearchData();
  },
  onLower:function(e){
    var pageIndex = this.data.pageIndex + 1;
    this.setData({pageIndex:pageIndex});
    this.getSearchData();
  },
  getSearchData:function() {
    this.setData({loadFlag:true});
    var keyword = this.data.keyword;
    var pageSize = this.data.pageSize;
    var pageIndex = this.data.pageIndex;
    console.log('pageIndex'+pageIndex);
    if (keyword == ''){
      this.setData({ loadFlag: false });
      return;
    } 
    var results = searchData.searchGoods.filter(function (e) { return e.goodsName.includes(keyword); });
    console.log('results count:'+results.length);
    var count = results.length;
    if (pageIndex>(count/pageSize)+1){
      this.setData({ loadFlag: false ,noData:true});
      return;
    }
    var start = (pageIndex - 1) * pageSize;
    var end = start + pageSize;
    results = results.slice(start,end);
    console.log(results);
    results = this.data.searchGoods.concat(results);
    this.setData({ searchGoods: results });
    this.setData({ loadFlag: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.searchGoods=[];
    this.getSearchData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})